package cn.xorange.commons.system.controller.platform;

import cn.xorange.commons.core.basic.bean.PageBean;
import cn.xorange.commons.core.basic.bean.ResultBean;
import cn.xorange.commons.core.basic.controller.DataController;
import cn.xorange.commons.security.annotation.Authorize;
import cn.xorange.commons.security.annotation.PassToken;
import cn.xorange.commons.system.entity.platform.SysMenu;
import cn.xorange.commons.system.service.platform.ISysMenuService;
import cn.xorange.commons.utils.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


/**
 * @author : yangjian
 * date : 2021/12/23
 * description : 平台菜单管理
 */
@RestController
@RequestMapping("/platform/menu")
@Authorize(hasPerm = "platform:menu")
public class SysMenuController extends DataController<SysMenu, ISysMenuService> {



    @Override
    @Authorize(hasPerm = "add")
    @PostMapping()
    @Transactional(rollbackFor = RuntimeException.class)
    public ResultBean add(@Validated @RequestBody SysMenu menu){
        if(Objects.isNull(menu.getParentId())){
            menu.setParentId(0L);
        }
        if(Objects.isNull(menu.getMenuName())){
            return ResultBean.onFail("菜单功能名称不能为空");
        }

        boolean ck = service.checkMenuNameUnique(menu);
        if (ck){
            return ResultBean.onFail("已有相同名称菜单功能");
        }


        return ResultBean.onSuccess(this.service.add(menu));
    }


    @PassToken
    @GetMapping("/plus")
    public PageBean myPlus(SysMenu entity) {
        super.startPage();
        entity.getQueryWrapper()
                .eq(!StringUtils.isEmpty(entity.getStatus()), "status", entity.getStatus())
                .eq(Objects.nonNull(entity.getSort()), "sort", entity.getSort())
                .ge(Objects.nonNull(entity.getParams().get("startTime")), "create_time", entity.getParams().get("startTime"))
                .lt(Objects.nonNull(entity.getParams().get("endTime")), "create_time", entity.getParams().get("endTime"));

        List<SysMenu> list = service.getListPlus(entity);
        return new PageBean(list);
    }

    //@Override
    //@Authorize(hasPerm = "remove")
    //@DeleteMapping("/{ids}")
    //@Transactional(rollbackFor = RuntimeException.class)
    //public ResultBean del(@PathVariable Long[] ids){
    //
    //    SysMenu check = new SysMenu();
    //    check.where.in("parentId", ids);
    //    List<SysMenu> childSysMenus = this.service.getList(check);
    //    if (childSysMenus.isEmpty()){
    //        return ResultBean.onSuccess(this.service.deleteById(ids));
    //    }else {
    //        return ResultBean.onFail("请先删除下级菜单功能");
    //    }
    //}
}
